<template>
  <div class="change">
    <div class="header">
      <span>新增岗位</span>
      <el-button class="button" @click="handleBack">返回</el-button>
    </div>
    <el-form :model="post" label-width="120px" :rules="rules" ref="editRef">
      <el-form-item label="岗位名称" prop="name">
        <el-input v-model="post.name" />
      </el-form-item>
      <el-form-item label="岗位简介" prop="job">
        <el-input v-model="post.job" type="textarea" />
      </el-form-item>
      <el-form-item label="主要职责">
        <div style="border: 1px solid #ccc">
          <Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" />
          <Editor
            style="height: 500px; overflow-y: hidden"
            v-model="valueHtml"
            @onCreated="handleCreated"
          />
        </div>
      </el-form-item>
      <el-form-item label="专家简介：">
        <div style="border: 1px solid #ccc">
          <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editorRef1"
          />
          <Editor
            style="height: 500px; overflow-y: hidden"
            v-model="valueHtml1"
            @onCreated="handleCreatedClick"
          />
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import "@wangeditor/editor/dist/css/style.css"; // 引入 css
import { onBeforeUnmount, ref, shallowRef, onMounted, reactive } from "vue";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { useRouter } from "vue-router";
import { useCareerStore } from "../../../store/career";
import dayjs from "dayjs";
import { ElMessage } from "element-plus";
import { useRoute } from "vue-router";
const store = useCareerStore();
const route = useRoute();
const router = useRouter();
//返回上一级

const handleBack = () => {
  router.back();
};
//富文本
const editorRef = shallowRef();
const editorRef1 = shallowRef();
// 内容 HTML
const valueHtml = ref("<p></p>");
const valueHtml1 = ref("<p></p>");
//创建实例
const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};
const handleCreatedClick = (editor1) => {
  editorRef1.value = editor1; // 记录 editor 实例，重要！
};
//初始化值
const post = reactive({
  name: "",
  job: "",
  valueHtml: "",
  valueHtml1: "",
});
const rules = reactive({
  name: [{ required: true, message: "岗位名称必填" }],
  job: [{ required: true, message: "岗位简介必填" }],

});
//添加岗位
const handleSave = () => {
  console.log(valueHtml1.value);
  const add = reactive({
    id: parseInt(Math.random() * 10),
    name: post.name,
    job: post.job,
    valueHtml: valueHtml.value,
    valueHtml1: valueHtml1.value,
    zip: dayjs(new Date()).format("YYYY年MM月DD日"),
  });
  console.log(valueHtml);
  if (post.name) {
    store.addmesaage(add);
    router.push("/career");
  } else {
    ElMessage("请添加完整信息");
  }
};
//编辑岗位
if (route.query.id) {
  const res = store.echo(route.query.id);
  post.name = res.name;
  post.job = res.job;
  valueHtml.value = res.valueHtml;
  valueHtml1.value = res.valueHtml1;
}
</script>

<style lang="less" scoped>
.header {
  width: 100%;
  margin: 8px 0;

  span {
    color: rgb(255, 153, 0);
    font-size: 16px;
    padding-left: 8px;
    font-weight: 800;
  }
  .button {
    float: right;
    background-color: rgb(255, 153, 0);
    color: #fff;
    margin-right: 8px;
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
:deep(.el-form) {
  margin-left: 10px;
}
:deep(.el-input) {
  width: 500px;
}
</style>
